<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let obj = {
      name: 'wsh',
      age: 18,
      color: [1,2,3,4],
      friend: {
        name: 'yxy'
      }
    }
    function deepClone(obj) {
      // obj不是数组或对象或者为空
      if (typeof obj !== 'object' || obj == null) {
        // 此时obj为基础数据类型,直接返回
        return obj
      }
      let result
      // 如果是数组
      if (obj instanceof Array) {
        result = []
      } else {
        result = {}
      }

      for (let key in obj) {
        // 深拷贝核心
        // 一层一层递归,直到值为原始类型
        // 只拷贝自身的属性
        if(obj.hasOwnProperty(key)) {
          result[key] = deepClone(obj[key])
        }
      }

      return result
    }
  </script>
</body>
</html>